package it.ap.sa.core.confservice.event;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

/**
 * @author Alessandro Paolillo
 */
@Component
public class ApplicationEnvironmentShutdown implements ApplicationListener<ContextClosedEvent> {

	private static final Logger logger = LoggerFactory.getLogger(ApplicationEnvironmentShutdown.class.getName());

	/** necessario per evitare di rifare il tutto **/
	static Boolean stopped = false;
	
	/**
	 * This method is called during Spring's shutdown.
	 * 
	 * @param event
	 *            Event raised when an ApplicationContext gets stopped.
	 */
	@Override
	public void onApplicationEvent(ContextClosedEvent event) {
		if(!stopped){
			logger.info(">>>>>>>>>>>>>>>>>>>>>>> Shutdown dell'ambiente ... IN CORSO <<<<<<<<<<<<<<<<< ");
			
			logger.info("");
			
			logger.info("---------------------------------------------------------------------------------------");
			logger.info("---------------------------------------------------------------------------------------");
			
			/** ricorda inizializzazione **/
			stopped = true;
			
			logger.info("");
			
			logger.info(">>>>>>>>>>>>>>>>>>>>>>> Shutdown dell'ambiente ... TERMINATO <<<<<<<<<<<<<<<<< ");
		}
	}
}
